package com.enterprise.projectManager.service.implemention;

import com.enterprise.projectManager.model.Problem;
import com.enterprise.projectManager.rope.ProblemRope;
import com.enterprise.projectManager.service.ProblemService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional
public class ProblemImpl implements ProblemService {
    private final ProblemRope problemRope;
    private final CountServiceImpl countService;
    @Override
    public Problem createProb(Problem problem) {
        if (problem.getProblemId() == null) problem.setProblemId(countService.generalProblemId());
        return problemRope.save(problem);
    }

    @Override
    public Boolean deleteProb(String id2) {
        Optional<Problem> optionalProblem = problemRope.findById(id2);
        problemRope.delete(optionalProblem.get());
        return Boolean.TRUE;    //待进一步判断
    }

    @Override
    public Problem updateProb(Problem problem) {
        return problemRope.save(problem);
    }

    @Override
    public List<Problem> searchProbByProjectName(String projectName) {
        return problemRope.findByProjectProjectName(projectName); // 直接返回所有匹配的合同列表
    }
    @Override
    public List<Problem> searchProbByProblemState(String problemState) {
        return problemRope.findByProblemState(problemState); // 直接返回所有匹配的合同列表
    }
    @Override
    public Problem searchProbBySendCompany(String problemSendcompany) {
        Optional<Problem> optionalProblem = problemRope.findByProblemSendcompany(problemSendcompany);
        return optionalProblem.orElse(null); // 或者根据实际需求返回其他默认值
    }


    @Override
    public List<Problem> listProb(int limitprob) {
        // 从数据库中检索前 limit 个项目
        return problemRope.findAll(PageRequest.of(0, limitprob)).getContent();
    }

    @Override
    public List<Problem> listAll() {
        return problemRope.findAll();
    }
}
